from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

from backend.models import PoppingVideo


# video新增接口
@csrf_exempt
def video_add(request):
    response = {}
    try:
        video_title = request.GET.get('video_title')
        video_path = request.GET.get('video_path')
        video_img = request.GET.get('video_img')
        PoppingVideo.objects.create(video_title=video_title, video_img=video_img, video_path=video_path)
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respCode'] = '999999'
        response['respMsg'] = str(e)
    return JsonResponse(response)


# video查询接口
@csrf_exempt
def video_info(request):
    response = {}
    try:
        if request.GET.get('video_title'):
            info = PoppingVideo.objects.filter(video_title=request.GET.get('video_title')).all()
        elif request.GET.get('searchInput'):
            info = PoppingVideo.objects.filter(video_title=request.GET.get('searchInput')).all()
        elif request.GET.get('video_id'):
            info = PoppingVideo.objects.filter(id=request.GET.get('video_id'))
        else:
            info = PoppingVideo.objects.all()
        response['list'] = list(info.values())
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respMsg'] = str(e)
        response['respCode'] = '999999'
    return JsonResponse(response)


@csrf_exempt
def video_del(request):
    response = {}
    try:
        if request.GET.get('id'):
            PoppingVideo.objects.filter(id=request.GET.get('id')).delete()
        response['respCode'] = '000000'
        response['respMsg'] = 'success'
    except Exception as e:
        response['respCode'] = '999999'
        response['respMsg'] = str(e)
    return JsonResponse(response)
